<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="设备ID" prop="topId">
                <el-input v-model="queryParams.topId" placeholder="请输入设备ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="unitID" prop="machineId">
                <el-input v-model="queryParams.machineId" placeholder="请输入unitID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备号" prop="moveId">
                <el-input v-model="queryParams.moveId" placeholder="请输入设备号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="制造区域" prop="area">
                <el-input v-model="queryParams.area" placeholder="请输入制造区域" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="主站点" prop="mainStep">
                <el-input v-model="queryParams.mainStep" placeholder="请输入主站点" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="实站点" prop="realStep">
                <el-input v-model="queryParams.realStep" placeholder="请输入实站点" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['SYSConfig:astSampleInfo:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList">
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </right-toolbar>
        </el-row>

        <t-ag-grid
          :style="{height: height}"
          :column-defs="column"
          :row-data="astSampleInfoList"
          :row-buffer="20"
          row-selection="multiple"
          :suppress-row-click-selection="true"
          :row-multi-select-with-click="true"
          align="center"
          header-align="center"
          :defaultColDef = "{
              menuTabs: ['filterMenuTab', 'columnsMenuTab']
          }"
          @selection-changed="selectionChanged"
          />

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改设备AST中样品站点信息对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="astSampleInfoRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="设备ID" prop="topId">
                    <el-input v-model="form.topId" placeholder="请输入设备ID" />
                </el-form-item>
                <el-form-item label="unitID" prop="machineId">
                    <el-input v-model="form.machineId" placeholder="请输入unitID" />
                </el-form-item>
                <el-form-item label="设备号" prop="moveId">
                    <el-input v-model="form.moveId" placeholder="请输入设备号" />
                </el-form-item>
                <el-form-item label="制造区域" prop="area">
                    <el-input v-model="form.area" placeholder="请输入制造区域" />
                </el-form-item>
                <el-form-item label="产品型号" prop="productId">
                    <el-input v-model="form.productId" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="主站点" prop="mainStep">
                    <el-input v-model="form.mainStep" placeholder="请输入主站点" />
                </el-form-item>
                <el-form-item label="实站点" prop="realStep">
                    <el-input v-model="form.realStep" placeholder="请输入实站点" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="AstSampleInfo">
  import { listAstSampleInfo, getAstSampleInfo, delAstSampleInfo, addAstSampleInfo, updateAstSampleInfo } from "@/api/SYSConfig/astSampleInfo"

  const { proxy } = getCurrentInstance()

  const astSampleInfoList = ref([])
  const open = ref(false)
  const loading = ref(true)
  const showSearch = ref(true)
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const total = ref(0)
  const title = ref("")

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10000,
      topId: null,
      machineId: null,
      moveId: null,
      area: null,
      modelType: null,
      machineType: null,
      productId: null,
      mainStep: null,
      realStep: null
    },
    rules: {
      topId: [
        { required: true, message: "设备ID不能为空", trigger: "blur" }
      ],
      machineId: [
        { required: true, message: "unitID不能为空", trigger: "blur" }
      ],
      moveId: [
        { required: true, message: "设备号不能为空", trigger: "blur" }
      ]
    }
  })

  const { queryParams, form, rules } = toRefs(data)

  const height = ref('calc(100% - 120px)')

  const column = [
    { headerName: "设备ID", field: "topId", headerCheckboxSelection: true, checkboxSelection: true, minWidth: 150, flex: 1 },
    { headerName: "unitID", field: "machineId", minWidth: 150, flex: 1 },
    { headerName: "设备号", field: "moveId", minWidth: 150, flex: 1 },
    { headerName: "制造区域", field: "area", minWidth: 70, flex: 1 },
    { headerName: "制造类型", field: "modelType", minWidth: 70, flex: 1 },
    { headerName: "设备类型", field: "machineType", minWidth: 70, flex: 1 },
    { headerName: "产品型号", field: "productId", minWidth: 150, flex: 1 },
    { headerName: "主站点", field: "mainStep", minWidth: 100, flex: 1 },
    { headerName: "实站点", field: "realStep", minWidth: 100, flex: 1 }
  ]

  column.forEach((item) => {
    item.filter = 'agMultiColumnFilter'
  })

  const gridApi = ref()

  const selectionChanged  = (params) => {
    gridApi.value = params.api
    const selectedRows = params.api.getSelectedRows()
    single.value = selectedRows.length !== 1
    multiple.value = !selectedRows.length
  }

  /** 查询设备AST中样品站点信息列表 */
  function getList() {
    loading.value = true
    listAstSampleInfo(queryParams.value).then(response => {
      astSampleInfoList.value = response.rows
      total.value = response.total
      loading.value = false
    })
  }

  // 取消按钮
  function cancel() {
    open.value = false
    reset()
  }

  // 表单重置
  function reset() {
    form.value = {
      topId: null,
      machineId: null,
      moveId: null,
      area: null,
      modelType: null,
      machineType: null,
      productId: null,
      mainStep: null,
      realStep: null
    }
    proxy.resetForm("astSampleInfoRef")
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset()
    open.value = true
    title.value = "添加设备AST中样品站点信息"
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset()
    const _topId = gridApi.value.getSelectedRows()[0].topId
    getAstSampleInfo(_topId).then(response => {
      form.value = response.data
      open.value = true
      title.value = "修改设备AST中样品站点信息"
    })
  }

  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["astSampleInfoRef"].validate(valid => {
      if (valid) {
        if (form.value.topId != null) {
          updateAstSampleInfo(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功")
            open.value = false
            getList()
          })
        } else {
          addAstSampleInfo(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
        }
      }
    })
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _topIds = []
    gridApi.value.getSelectedRows().map(item => _topIds.push(item.topId) )
    proxy.$modal.confirm('是否确认删除设备AST中样品站点信息编号为"' + _topIds + '"的数据项？').then(function () {
      return delAstSampleInfo(_topIds)
    }).then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('SYSConfig/astSampleInfo/export', {
      ...queryParams.value
    }, `astSampleInfo_${new Date().getTime()}.xlsx`)
  }

  getList()
</script>
